package com.dodock.banglapapers.server.helper;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.SAXException;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListManobZamin implements DDNNewsListInterface{

	@Override
	public String GenerateNewsList(String categoryid) throws IOException {
		
		if(categoryid.equals("top-news")){
			//http://www.mzamin.com/index.php
			//Pull আরও খবর + Middle + Right columns news
			return index__box_more_middle_right("http://www.mzamin.com/index.php");
		}
		else if(categoryid.equals("first-page")){
			//http://www.mzamin.com/sectionnews.php?secid=MTg=
			//Box news and Right column news
			return news__box_right("http://www.mzamin.com/sectionnews.php?secid=MTg=");
		}
		else if(categoryid.equals("exclusive")){
			//http://www.mzamin.com/sectionnews.php?secid=MTk=
			//Box news and Right column news
			return news__box_right("http://www.mzamin.com/sectionnews.php?secid=MTk=");
		}
		else if(categoryid.equals("national")){
			//http://www.mzamin.com/section_od.php?secid=MjU=
			//Box news and Right column news
			return od__box_right("http://www.mzamin.com/section_od.php?secid=MjU=");
		}
		else if(categoryid.equals("bangla")){
			//http://www.mzamin.com/sectionnews.php?secid=MjA=
			//Box news and Right column news
			return news__box_right("http://www.mzamin.com/sectionnews.php?secid=MjA=");
		}
		else if(categoryid.equals("world")){
			//http://www.mzamin.com/sectionnews.php?secid=MjE=
			//Box news and Right column news
			return news__box_right("http://www.mzamin.com/sectionnews.php?secid=MjE=");
		}
		else if(categoryid.equals("entertainment")){
			//http://www.mzamin.com/sectionnews_ent.php
			//Box news & Right column News
			return ent__box_right("http://www.mzamin.com/sectionnews_ent.php");
		}
		else if(categoryid.equals("sports")){
			//http://www.mzamin.com/sectionnews_sport.php
			//Box news & the right column news
			return sport__box_right("http://www.mzamin.com/sectionnews_sport.php");
		}
		else if(categoryid.equals("others")){
			//http://www.mzamin.com/section_od.php?secid=MjY=
			//Box News only
			return od__box("http://www.mzamin.com/section_od.php?secid=MjY=");
		}
		else if(categoryid.equals("business")){
			//http://www.mzamin.com/section_od.php?secid=MjQ=
			//Only the Box news
			return od__box("http://www.mzamin.com/section_od.php?secid=MjQ=");
		}
		else if(categoryid.equals("last-page")){
			//http://www.mzamin.com/sectionnews.php?secid=Mjc=
			//Box news & the right column news
			return news__box_right("http://www.mzamin.com/sectionnews.php?secid=Mjc=");
		}
		
		return "[]";
	}
	
	private String index__box_more_middle_right(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements top_left_inners = doc.select("div[id=top_left_inner]");
			if(top_left_inners != null && top_left_inners.size() > 0){
				for (Element elem : top_left_inners) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					if(top_title == null){
						continue;
					}
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					
					if(list.contains("id")){
						continue;
					}
					
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					
					list.add(id);
					array.add(news);
					
					
				}
			}
			
			//middle
			Element top_left_inner2 = doc.getElementById("top_left_inner2");
			Elements top_left_smallimgs = top_left_inner2.select("div[id=top_left_smallimg]");
			Elements listnewss = top_left_inner2.select("div[id=listnews]");
			for(int i=0; i<top_left_smallimgs.size()-1; i++){
				Element img_ = top_left_smallimgs.get(i);
				Element ttl_ = listnewss.get(i);
				JsonObject news = new JsonObject();
				
				String newslink = ttl_.getElementsByTag("a").get(0).attr("href");
				newslink = newslink.replace("details.php?nid=", "");
				String []linkptz = newslink.split("&");
				String id = linkptz[0];
				news.addProperty("id", id);
				
				news.addProperty("title", ttl_.text());
				
				if(img_ != null){
					Elements imgs = img_.getElementsByTag("img");
					if(imgs != null && imgs.size() > 0){
						Element img = imgs.get(0);
						news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
					}
				}
				
				array.add(news);
			}
			
			//right
			Elements top_right_spans = doc.select("div[id=top_right_span]");
			if(top_right_spans != null && top_right_spans.size() > 0){
				for (Element elem : top_right_spans) {
					JsonObject news = new JsonObject();
					
					Element top_right_title_a = elem.getElementById("top_right_title").getElementsByTag("a").get(0);
					String newslink = top_right_title_a.attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", top_right_title_a.text());
					
					Element top_right_img = elem.getElementById("top_right_img");
					if(top_right_img != null){
						Elements imgs = top_right_img.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					array.add(news);
				}
			}
			
			//more
			Element mores = top_left_inners.last();
			Elements lis = mores.getElementsByTag("li");
			if(lis != null && lis.size() > 0){
				for (Element elem : lis) {
					JsonObject news = new JsonObject();
					
					Element li_a = elem.getElementsByTag("a").get(0);
					String newslink = li_a.attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", li_a.text());
					
					
					array.add(news);
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}
	
	private String ent__box_right(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements top_left_inners = doc.select("div[id=top_left_inner]");
			if(top_left_inners != null && top_left_inners.size() > 0){
				for (Element elem : top_left_inners) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					if(top_title == null){
						continue;
					}
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					
					if(list.contains(id)){
						continue;
					}
					
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					
					list.add(id);
					array.add(news);
					
					
				}
			}
			
			//middle
			Element top_left_inner2 = doc.getElementById("top_left_inner2");
			Elements top_left_smallimgs = top_left_inner2.select("div[id=top_left_smallimg]");
			Elements listnewss = top_left_inner2.select("div[id=listnews]");
			for(int i=0; i<top_left_smallimgs.size(); i++){
				Element img_ = top_left_smallimgs.get(i);
				Element ttl_ = listnewss.get(i);
				JsonObject news = new JsonObject();
				
				String newslink = ttl_.getElementsByTag("a").get(0).attr("href");
				newslink = newslink.replace("details.php?nid=", "");
				String []linkptz = newslink.split("&");
				String id = linkptz[0];
				
				if(list.contains(id)){
					continue;
				}
				
				news.addProperty("id", id);
				
				news.addProperty("title", ttl_.text());
				
				if(img_ != null){
					Elements imgs = img_.getElementsByTag("img");
					if(imgs != null && imgs.size() > 0){
						Element img = imgs.get(0);
						news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
					}
				}
				
				list.add(id);
				array.add(news);
			}
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}
	
	private String sport__box_right(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements top_left_inners = doc.select("div[id=top_left_inner]");
			if(top_left_inners != null && top_left_inners.size() > 0){
				for (Element elem : top_left_inners) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					if(top_title == null){
						continue;
					}
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					
					if(list.contains(id)){
						continue;
					}
					
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					
					list.add(id);
					array.add(news);
					
					
				}
			}
			
			//middle
			Element top_left_inner2 = doc.getElementById("top_left_inner2");
			Elements top_left_smallimgs = top_left_inner2.select("div[id=top_left_smallimg]");
			Elements listnewss = top_left_inner2.select("div[id=listnews]");
			for(int i=0; i<top_left_smallimgs.size(); i++){
				Element img_ = top_left_smallimgs.get(i);
				Element ttl_ = listnewss.get(i);
				JsonObject news = new JsonObject();
				
				String newslink = ttl_.getElementsByTag("a").get(0).attr("href");
				newslink = newslink.replace("details.php?nid=", "");
				String []linkptz = newslink.split("&");
				String id = linkptz[0];
				
				if(list.contains(id)){
					continue;
				}
				
				news.addProperty("id", id);
				
				news.addProperty("title", ttl_.text());
				
				if(img_ != null){
					Elements imgs = img_.getElementsByTag("img");
					if(imgs != null && imgs.size() > 0){
						Element img = imgs.get(0);
						news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
					}
				}
				
				list.add(id);
				array.add(news);
			}
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}
	
	
	private String news__box_right(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements top_left_inners = doc.select("div[id=top_left_inner]");
			if(top_left_inners != null && top_left_inners.size() > 0){
				for (Element elem : top_left_inners) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					if(top_title == null){
						continue;
					}
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					array.add(news);
				}
			}
			
			
			//right
			Elements top_right_spans = doc.select("div[id=top_right_span]");
			if(top_right_spans != null && top_right_spans.size() > 0){
				for (Element elem : top_right_spans) {
					JsonObject news = new JsonObject();
					
					Element top_right_title_a = elem.getElementById("top_right_title").getElementsByTag("a").get(0);
					String newslink = top_right_title_a.attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", top_right_title_a.text());
					
					Element top_right_img = elem.getElementById("top_right_img");
					if(top_right_img != null){
						Elements imgs = top_right_img.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					array.add(news);
				}
			}
			
			
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}
	
	private String od__box(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements othersection__divs = doc.select("div[id=othersection__div]");
			if(othersection__divs != null && othersection__divs.size() > 0){
				for (Element elem : othersection__divs) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					array.add(news);
				}
			}
			
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}
	
	private String od__box_right(String url){
		try{
			JsonArray array = new JsonArray();
			ArrayList<String> list = new ArrayList<String>();
			Document doc = Jsoup.connect(url).get();
			
			//box
			Elements othersection__divs = doc.select("div[id=othersection__div]");
			if(othersection__divs != null && othersection__divs.size() > 0){
				for (Element elem : othersection__divs) {
					JsonObject news = new JsonObject();
					
					Element top_title = elem.getElementById("top_title");
					String newslink = top_title.getElementsByTag("a").get(0).attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", top_title.text());
					
					Element top_left_image = elem.getElementById("top_left_image");
					if(top_left_image != null){
						Elements imgs = top_left_image.getElementsByTag("img");
						if(imgs != null && imgs.size() > 0){
							Element img = imgs.get(0);
							news.addProperty("image", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
						}
					}
					array.add(news);
				}
			}
			
			
			//right
			Element mores = doc.getElementById("top_left_inner2_od");
			Elements lis = mores.getElementsByTag("li");
			if(lis != null && lis.size() > 0){
				for (Element elem : lis) {
					JsonObject news = new JsonObject();
					
					Element li_a = elem.getElementsByTag("a").get(0);
					String newslink = li_a.attr("href");
					newslink = newslink.replace("details.php?nid=", "");
					String []linkptz = newslink.split("&");
					String id = linkptz[0];
					news.addProperty("id", id);
					
					news.addProperty("title", li_a.text());
					
					
					array.add(news);
				}
			}
			
			
			return array.toString();
		}
		catch (Exception e) {
		}
		return "[]";
	}	
	
}
